package com.qiongyou.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qiongyou.activity.R;
import com.qiongyou.utils.AsyncImageLoader;
import com.qiongyou.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by aaa on 15-3-23.
 */
public class GridAdapter extends BaseAdapter {
    private List<Map<String,String>> data;
    private Context context;

    public GridAdapter(List<Map<String,String>> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view= LayoutInflater.from(context).inflate(R.layout.grid,viewGroup,false);
            view.setTag(new ViewHolder(view));
        }
        ViewHolder holder= (ViewHolder) view.getTag();
        data=new ArrayList<Map<String, String>>();
        //设置标题
        holder.title.setText(data.get(i).get("name"));
        //设置国家
        holder.name.setText(data.get(i).get("name"));
        //设置主题
        holder.total_threads.setText(data.get(i).get("total_threads"));

        //获取图片地址
        String imagerUrl=data.get(i).get("photo");
        if (imagerUrl!=null) {
            Bitmap bm = HttpUtils.getBitmapFormUrl(imagerUrl);
            holder.photo.setImageBitmap(bm);
        }
        return view;
    }
    public static class ViewHolder{
        private View itemView;
        private ImageView photo;
        private TextView name;
        private TextView title;
        private TextView total_threads;

        public ViewHolder(View itemView){
           this.itemView=itemView;
           title= ((TextView) itemView.findViewById(R.id.grid_title));
           photo= ((ImageView) itemView.findViewById(R.id.grid_imageView));
           name= ((TextView) itemView.findViewById(R.id.grid_state));
           total_threads= ((TextView) itemView.findViewById(R.id.grid_style));
        }
    }
}
